<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">任务计划</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!home_console">首页</a>
          <a><cite>任务计划</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：
            <input id="schedule-search-value" class="layui-input search-input" type="text" placeholder="输入关键字"/>&emsp;
            <button id="schedule-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
        </div>

        <table class="layui-table" id="schedule-table" lay-filter="schedule-table"></table>
    </div>
</div>

<!-- 表格状态列 -->
<script type="text/html" id="schedule-tpl-status">
    <span class="layui-badge layui-bg-orange">{{d.statusStr}}</span>
</script>

<!-- 表格操作列 -->
<script type="text/html" id="schedule-table-bar">
    <div class="layui-inline" lay-event="add" lay-tips="新增"><i class="layui-icon layui-icon-add-1"></i></div>
    <div class="layui-inline" lay-event="edit" lay-tips="修改"><i class="layui-icon layui-icon-edit"></i></div>
    <div class="layui-inline" lay-event="del" lay-tips="删除"><i class="layui-icon layui-icon-delete"></i></div>
    <div class="layui-inline" lay-event="runOrResume" lay-tips="运行|恢复"><i class="layui-icon layui-icon-play"></i></div>
    <div class="layui-inline" lay-event="pause" lay-tips="暂停"><i class="layui-icon layui-icon-pause"></i></div>
</script>

<script>
    layui.use(['form', 'table', 'util', 'admin', 'element', 'jquery'], function () {
        const $ = layui.jquery;
        const table = layui.table;
        const layer = layui.layer;
        const admin = layui.admin;

        // 渲染表格
        table.render({
            elem: '#schedule-table',
            url: basePath + 'batch/schedule/list',
            toolbar: '#schedule-table-bar',
            defaultToolbar: ['filter'],
            page: true,
            cols: [
                [
                    {type: 'radio', fixed: 'left'},
                    {field: 'name', title: '计划名称', width: 150, fixed: 'left', sort: true},
                    {field: 'code', title: '计划编码', width: 150, sort: true},
                    {field: 'cronTypeStr', title: '表达式类型', width: 150, sort: true},
                    {field: 'cronExpression', title: '执行表达式', width: 150, sort: true},
                    {field: 'taskCode', title: '任务编码', width: 150, sort: true},
                    {field: 'taskName', title: '任务名称', width: 150, sort: true},
                    {field: 'statusStr', title: '状态', templet: '#schedule-tpl-status', width: 120, sort: true},
                    {field: 'interfaceName', title: '业务类', width: 150, sort: true},
                    // {field: 'serverIp', title: '运行服务器', width: 150, sort: true},
                    {field: 'startDate', title: '最近开始时间', width: 150, sort: true},
                    {field: 'endDate', title: '最近结束时间', width: 150, sort: true},
                    {field: 'createDate', title: '创建时间', width: 150, sort: true},
                    {field: 'updateDate', title: '更新时间', width: 150, sort: true},
                ]
            ]
        });


        // 工具条点击事件
        table.on('toolbar(schedule-table)', function (obj) {
            const checkStatus = table.checkStatus(obj.config.id); //获取选中行状态
            const data = checkStatus.data;  //获取选中行数据

            switch (obj.event) {
                case 'add':
                    addOrEdit();
                    break;
                case 'edit':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                        return;
                    }
                    addOrEdit(data[0]);
                    break;
                case 'del':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                        return;
                    }
                    del(data[0].id);
                    break;
                case 'runOrResume':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                        return;
                    }
                    runOrResume(data[0]);
                    break;
                case 'pause':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                        return;
                    }
                    pause(data[0].id);
                    break;
            }
        });


        /**
         * 新增|修改
         * @param data
         */
        const addOrEdit = function (data) {
            const title = data ? '修改任务计划' : '添加任务计划';
            admin.putTempData('t_schedule', data);
            admin.popupCenter({
                title: title,
                area: '1050px',
                offset: '50px',
                path: basePath + 'batch/schedule/editForm',
                finish: function () {
                    table.reload('schedule-table', {});
                }
            });
        };


        const del = function (id) {
            layer.confirm('确定要删除吗？', function () {
                layer.load(2);
                $.post(basePath + 'batch/schedule/delete', {
                    scheduleId: id
                }, function (data) {
                    layer.closeAll('loading');
                    if (data.success) {
                        layer.msg(data.msg, {icon: 1});
                        table.reload('schedule-table', {});
                    } else {
                        layer.msg(data.msg, {icon: 2});
                    }
                });
            });
        };


        const runOrResume = function (date) {
            if (2 === date.status) {
                resume(date.id);
            } else {
                run(date.id);
            }
        };

        /**
         * 运行
         * @param id
         */
        const run = function (id) {
            layer.confirm('确定要【运行】该计划吗？', function () {
                layer.load(2);
                $.post(basePath + 'batch/schedule/run', {
                    scheduleId: id
                }, function (data) {
                    layer.closeAll('loading');
                    if (data.success) {
                        layer.msg(data.msg, {icon: 1});
                        table.reload('schedule-table', {});
                    } else {
                        layer.msg(data.msg, {icon: 2});
                    }
                });
            });
        };

        /**
         * 暂停
         * @param id
         */
        const pause = function (id) {
            layer.confirm('确定要【暂停】该计划吗？', function () {
                layer.load(2);
                $.post(basePath + 'batch/schedule/pause', {
                    scheduleId: id
                }, function (data) {
                    layer.closeAll('loading');
                    if (data.success) {
                        layer.msg(data.msg, {icon: 1});
                        table.reload('schedule-table', {});
                    } else {
                        layer.msg(data.msg, {icon: 2});
                    }
                });
            });

        };

        /**
         * 从暂停中恢复
         * @param id
         */
        const resume = function (id) {
            layer.confirm('确定要【恢复运行】该计划吗？', function () {
                layer.load(2);
                $.post(basePath + 'batch/schedule/resume', {
                    scheduleId: id
                }, function (data) {
                    layer.closeAll('loading');
                    if (data.success) {
                        layer.msg(data.msg, {icon: 1});
                        table.reload('schedule-table', {});
                    } else {
                        layer.msg(data.msg, {icon: 2});
                    }
                });
            });
        };

        // 搜索按钮点击事件
        $('#schedule-btn-search').click(function () {
            const value = $('#schedule-search-value').val();
            table.reload('schedule-table', {where: {searchValue: value}});
        });
    });
</script>